package com.xtourms.client.rm.hotel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyLabel;
import com.xtourms.shared.field.HotelField;

public class RoomManager extends Composite {

	private static RoomManagerUiBinder uiBinder = GWT
			.create(RoomManagerUiBinder.class);

	interface RoomManagerUiBinder extends UiBinder<Widget, RoomManager> {
	}

	@UiField FlexTable roomTable;
	private JSONObject hotelObj;
	private JSONArray roomsObj;
	private ContentFrame parent;
	
	public RoomManager(JSONObject hotelObj, JSONArray roomsObj, ContentFrame parent) {
		initWidget(uiBinder.createAndBindUi(this));
		this.hotelObj = hotelObj;
		this.roomsObj = roomsObj;
		this.parent = parent;
		initTable();
	}
	
	private void initTable() {
		int col = 0;
		roomTable.getCellFormatter().setWidth(0, col, "30px");
		roomTable.setText(0, col++, "选项");
		roomTable.getCellFormatter().setWidth(0, col, "60px");
		roomTable.setText(0, col++, "房型");
		roomTable.getCellFormatter().setWidth(0, col, "50px");
		roomTable.setText(0, col++, "底价");
		roomTable.getCellFormatter().setWidth(0, col, "50px");
		roomTable.setText(0, col++, "销售价");
		roomTable.getCellFormatter().setWidth(0, col, "50px");
		roomTable.setText(0, col++, "挂牌价");
		roomTable.getCellFormatter().setWidth(0, col, "40px");
		roomTable.setText(0, col++, "早餐");
		roomTable.getCellFormatter().setWidth(0, col, "242px");
		roomTable.setText(0, col++, "描述");
		roomTable.getCellFormatter().setWidth(0, col, "80px");
		roomTable.setText(0, col++, "起始日期");
		roomTable.getCellFormatter().setWidth(0, col, "80px");
		roomTable.setText(0, col++, "终止日期");
		roomTable.getRowFormatter().addStyleName(0, "result_header");

		if(null != roomsObj && roomsObj.size() > 0){
			for(int index = 0; index < roomsObj.size(); index++){
				fillRow(index+1, (JSONObject)roomsObj.get(index));
			}
		}
	}

	private void fillRow(int row, JSONObject room) {
		int col = 0;
		RadioButton rb = new RadioButton("room");
		roomTable.setWidget(row, col++, rb);
		roomTable.setText(row, col++, ((JSONString)room.get(HotelField.ROOM_STYLE)).stringValue());
		String bPrice = ((JSONNumber)room.get(HotelField.ROOM_BASE_PRICE)).toString();
		roomTable.setWidget(row, col++, new MoneyLabel(bPrice));
		roomTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)room.get(HotelField.ROOM_SELL_PRICE)).toString()));
		roomTable.setWidget(row, col++, new MoneyLabel(((JSONNumber)room.get(HotelField.ROOM_ACTUAL_PRICE)).toString()));
		roomTable.setText(row, col++, ((JSONString)room.get(HotelField.ROOM_BREAKFAST)).stringValue());
		roomTable.setText(row, col++, ((JSONString)room.get(HotelField.ROOM_DES)).stringValue());
		roomTable.setText(row, col++, ((JSONString)room.get(HotelField.ROOM_START_DATE)).stringValue());
		roomTable.setText(row, col++, ((JSONString)room.get(HotelField.ROOM_END_DATE)).stringValue());
		if(0 == row%2){
			roomTable.getRowFormatter().addStyleName(row, "odd");
		}else{
			roomTable.getRowFormatter().addStyleName(row, "even");
		}
	}
	
	@UiHandler("add")
	void addRoom(ClickEvent evt){
		parent.appendContent(new RoomDetailManager(hotelObj, parent, new AsyncCallback<String>() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onSuccess(String result) {
				JSONObject roomObj = (JSONObject) JSONParser.parse(result);
				roomsObj.set(roomsObj.size(), roomObj);
				fillRow(roomsObj.size(), roomObj);
				parent.resumeContent();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError("添加房间发生错误"+caught.getMessage());
			}
		}));
	}
	
	@UiHandler("modify")
	void modifyRoom(ClickEvent evt){
		int index = 0;
		JSONObject roomObj = null;
		if(null != roomsObj && roomsObj.size() > 0){
			for(; index < roomsObj.size(); index++){
				RadioButton rb = (RadioButton)roomTable.getWidget(index+1, 0);
				if(Boolean.TRUE == rb.getValue()){
					roomObj = (JSONObject)roomsObj.get(index);
					break;
				}
			}
		}
		if( null != roomObj){
			final int modifyIndex = index;
			parent.appendContent(new RoomDetailManager(hotelObj, roomObj, parent, new AsyncCallback<String>() {

				@Override
				public void onFailure(Throwable caught) {
					MessageBox.ins.showError("添加房间发生错误"+caught.getMessage());
				}

				@SuppressWarnings("deprecation")
				@Override
				public void onSuccess(String result) {
					JSONObject roomObj = (JSONObject) JSONParser.parse(result);
					roomsObj.set(modifyIndex, roomObj);
					fillRow(modifyIndex+1, roomObj);
					parent.resumeContent();
				}
			}));
		}
	}
	
	@UiHandler("back")
	void back(ClickEvent evt){
		parent.resumeContent();
	}

}
